﻿Imports System.IO
Imports System.Threading
Public Class frmBpipprm

    Private DosCommandOutput As DosCommand.DosCommandOutput '执行Dos程序类
    Private BpipprmGale As New CAermod.Bpipprm
    Property Bpip() As CAermod.Bpipprm
        Get
            Return Me.BpipprmGale
        End Get
        Set(ByVal value As CAermod.Bpipprm)
            Me.BpipprmGale = value
        End Set
    End Property

    Private Sub Formbprm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.EFlexBuiName.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.EFlexCoor.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.EFlexFloor.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.EFlexPSouce.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)

        Me.ComboBox1.Text = Me.BpipprmGale.Process
        TxtTtle.Text = Me.BpipprmGale.Title
        TxtUnitsN.Text = Me.BpipprmGale.UntisName
        CTunitV.Value = Me.BpipprmGale.UntisVale
        TxtGridN.Text = Me.BpipprmGale.GridName
        CTGridV.Value = Me.BpipprmGale.GridVale

        '建筑物
        BCribil()

        '每个建筑物有几层
        BFcrib()

        '每层的顶角坐标
        EFlexCoo()

        '污染源
        PSou()

        Me.EFlexBuiName.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexBuiName.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexBuiName.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.EFlexCoor.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexCoor.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexCoor.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.EFlexFloor.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexFloor.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexFloor.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None

        Me.EFlexPSouce.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.EFlexPSouce.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.EFlexPSouce.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None
    End Sub
    ''' <summary>
    ''' 填充污染源EFlexPSouce控件
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub PSou()
        Me.EFlexPSouce.Rows.Count = Me.BpipprmGale.ArryBuStack.Length + 1
        For j As Integer = 0 To Me.BpipprmGale.ArryBuStack.Length - 1
            Me.EFlexPSouce.SetData(j + 1, 0, Me.BpipprmGale.ArryBuStack(j).BrandName)
            Me.EFlexPSouce.SetData(j + 1, 1, Me.BpipprmGale.ArryBuStack(j).BrandBase)
            Me.EFlexPSouce.SetData(j + 1, 2, Me.BpipprmGale.ArryBuStack(j).BrandHight)
            Me.EFlexPSouce.SetData(j + 1, 3, Me.BpipprmGale.ArryBuStack(j).BrandX)
            Me.EFlexPSouce.SetData(j + 1, 4, Me.BpipprmGale.ArryBuStack(j).BrandY)
        Next
    End Sub

    ''污染源,赋值
    'Private Sub EFlexPSouce_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EFlexPSouce.Validating
    '    Dim L As Integer = Me.EFlexPSouce.Rows.Count - 1
    '    ReDim Me.BpipprmGale.ArryBuStack(L - 1)
    '    xxx(L)
    'End Sub
    '''' <summary>
    '''' 为污染源数组赋值,方法
    '''' </summary>
    '''' <param name="l"></param>
    '''' <remarks></remarks>
    'Private Sub xxx(ByVal l As Integer)
    '    For i As Integer = 0 To l - 1
    '        Me.BpipprmGale.ArryBuStack(i) = New CAermod.BuStack
    '        Me.BpipprmGale.ArryBuStack(i).BrandName = Me.EFlexPSouce.GetData(i + 1, 0)
    '        Me.BpipprmGale.ArryBuStack(i).BrandBase = Me.EFlexPSouce.GetData(i + 1, 1)
    '        Me.BpipprmGale.ArryBuStack(i).BrandHight = Me.EFlexPSouce.GetData(i + 1, 2)
    '        Me.BpipprmGale.ArryBuStack(i).BrandX = Me.EFlexPSouce.GetData(i + 1, 3)
    '        Me.BpipprmGale.ArryBuStack(i).BrandY = Me.EFlexPSouce.GetData(i + 1, 4)
    '    Next
    'End Sub
    '增加污染源
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ReDim Preserve Me.BpipprmGale.ArryBuStack(Me.BpipprmGale.ArryBuStack.Length)
        Me.BpipprmGale.ArryBuStack(Me.BpipprmGale.ArryBuStack.Length - 1) = New CAermod.BuStack
        PSou()
    End Sub
    '删除污染源一条记录
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim index As Integer = Me.EFlexPSouce.RowSel - 1
        If index >= 0 Then
            For i As Integer = index To Me.BpipprmGale.ArryBuStack.Length - 2
                Me.BpipprmGale.ArryBuStack(i) = Me.BpipprmGale.ArryBuStack(i + 1)
            Next
            ReDim Preserve Me.BpipprmGale.ArryBuStack(Me.BpipprmGale.ArryBuStack.Length - 2)
            PSou()
        End If
    End Sub
    '为建筑物数组赋值
    Private Sub EFlexBuiName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EFlexBuiName.Validating
        Dim L As Integer = Me.EFlexBuiName.Rows.Count - 1
        For i As Integer = 0 To L - 1
            Me.BpipprmGale.ArryBldgi(i).BuildStack = Me.EFlexBuiName.GetData(i + 1, 0)
            Me.BpipprmGale.ArryBldgi(i).BuildHight = Me.EFlexBuiName.GetData(i + 1, 1)
        Next
    End Sub
    '增加建筑物一条记录
    Private Sub BCriBild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCriBild.Click
        ReDim Preserve Me.BpipprmGale.ArryBldgi(Me.BpipprmGale.ArryBldgi.Length)
        Me.BpipprmGale.ArryBldgi(Me.BpipprmGale.ArryBldgi.Length - 1) = New CAermod.Bldgi
        BCribil()
    End Sub
    ''' <summary>
    ''' 填充EFlexBuiName建筑物
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BCribil()
        Me.EFlexBuiName.Rows.Count = Me.BpipprmGale.ArryBldgi.Length + 1
        For i As Integer = 0 To Me.BpipprmGale.ArryBldgi.Length - 1
            Me.EFlexBuiName.SetData(i + 1, 0, Me.BpipprmGale.ArryBldgi(i).BuildStack & i + 1)
            Me.EFlexBuiName.SetData(i + 1, 1, Me.BpipprmGale.ArryBldgi(i).BuildHight)
        Next
    End Sub

    '删除一条记录,建筑物
    Private Sub BDelBild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BDelBild.Click
        Dim index As Integer = Me.EFlexBuiName.RowSel - 1
        If index >= 0 Then
            For i As Integer = index To Me.BpipprmGale.ArryBldgi.Length - 2
                Me.BpipprmGale.ArryBldgi(i) = Me.BpipprmGale.ArryBldgi(i + 1)
            Next
            ReDim Preserve Me.BpipprmGale.ArryBldgi(Me.BpipprmGale.ArryBldgi.Length - 2)
            BCribil()
            If Me.EFlexFloor.Rows.Count > 1 Then
                'BFcrib()
                Me.EFlexFloor.Rows.Count = 1
                Me.EFlexCoor.Rows.Count = 1
            End If
        End If
    End Sub
    '为各层的数组赋值
    Private Sub EFlexFloor_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EFlexFloor.Validating
        If Me.BpipprmGale.ArryBldgi.Length > 0 Then
            Dim L As Integer = Me.EFlexFloor.Rows.Count - 1
            For i As Integer = 0 To L - 1
                Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(i).TireHight = Me.EFlexFloor.GetData(i + 1, 1)
            Next
        End If
    End Sub
    '增加一条记录,各层角的个数
    Private Sub BFCrbild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BFCrbild.Click
        If Me.BpipprmGale.ArryBldgi.Length > 0 Then
            ReDim Preserve Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire.Length)
            Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire.Length - 1) = New CAermod.BuildTire
            BFcrib()
        End If
    End Sub
    '单击EFlexBuiName事件,给数组赋值
    Private xx As Integer = 1
    Private Sub EFlexBuiName_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles EFlexBuiName.MouseClick
        If Me.EFlexBuiName.Rows.Count > 1 Then
            xx = Me.EFlexBuiName.RowSel
            BFcrib()
            EFlexFloor_MouseClick(sender, e)
        End If
    End Sub
    ''' <summary>
    ''' 设置EFlexFloor,各层角的个数
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BFcrib()
        If Me.EFlexBuiName.Rows.Count > 1 Then
            Me.EFlexFloor.Rows.Count = Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire.Length + 1
            For i As Integer = 0 To Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire.Length - 1
                Me.EFlexFloor.SetData(i + 1, 0, "第" & i + 1 & "层")
                Me.EFlexFloor.SetData(i + 1, 1, Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(i).TireHight)
            Next
        End If
    End Sub

    '单击EFlexCoor事件,给数组赋值
    Private ClickFoor As Integer = 1
    Private Sub EFlexFloor_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles EFlexFloor.MouseClick
        If Me.EFlexFloor.Rows.Count > 1 Then
            ClickFoor = Me.EFlexFloor.RowSel
            EFlexCoo()
        Else
            Me.EFlexCoor.Rows.Count = 1
        End If
    End Sub
    ''' <summary>
    ''' 设置EFlexCoor,角点坐标
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EFlexCoo()
        If Me.EFlexFloor.Rows.Count > 1 Then
            Me.EFlexCoor.Rows.Count = Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length + 1
            For i As Integer = 0 To Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length - 1
                Me.EFlexCoor.SetData(i + 1, 0, i + 1)
                Me.EFlexCoor.SetData(i + 1, 1, Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(i).x)
                Me.EFlexCoor.SetData(i + 1, 2, Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(i).y)
            Next
        End If
    End Sub

    '删除一条记录,各层角的个数
    Private Sub BFdel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BFdel.Click
        Dim index As Integer = Me.EFlexFloor.RowSel - 1
        If index >= 0 Then
            For i As Integer = index To Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire.Length - 2
                Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(i) = Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(i + 1)
            Next
            ReDim Preserve Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire.Length - 2)
            BFcrib()
            If Me.EFlexCoor.Rows.Count > 1 Then
                EFlexCoo()
            End If
            Me.EFlexCoor.Rows.Count = 1
        End If
    End Sub
    '增加一条记录,角点坐标
    Private Sub BCrooCr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCrooCr.Click
        If Me.EFlexFloor.Rows.Count > 1 Then
            ReDim Preserve Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length)
            Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length - 1) = New CAermod.BuildCorner
            EFlexCoo()
        End If
    End Sub
    '为数组赋值,点角坐标
    Private Sub EFlexCoor_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EFlexCoor.Validating
        If Me.EFlexFloor.Rows.Count > 1 Then
            Dim L As Integer = Me.EFlexCoor.Rows.Count - 1
            'ReDim Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(L - 1)
            For i As Integer = 0 To L - 1
                'Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(i) = New CAermod.BuildCorner
                Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(i).x = Me.EFlexCoor.GetData(i + 1, 1)
                Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(i).y = Me.EFlexCoor.GetData(i + 1, 2)
            Next
        End If
    End Sub
    '删除一条记录,角点坐标
    Private Sub BCrooDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCrooDel.Click
        Dim index As Integer = Me.EFlexCoor.RowSel - 1
        If index >= 0 And Me.EFlexFloor.RowSel - 1 >= 0 Then
            For i As Integer = index To Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length - 2
                Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(i) = Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(i + 1)
            Next
            ReDim Preserve Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length - 2)
            Me.EFlexCoo()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '是否存在建筑物,如没有返回
        If Me.BpipprmGale.ArryBldgi.Length <= 0 Then
            Return
        End If
        '判断每一个建筑物是几层，如没有提示
        Dim z As Integer = 0
        For i As Integer = 0 To Me.BpipprmGale.No - 1
            If Me.BpipprmGale.ArryBldgi(i).BuildCount < 1 Then
                MsgBox("请指出增加的建筑物的层数！")
                z = 1
                Exit For
            End If
        Next
        '判断每一层的顶角个数，如没有提示
        Dim p As Integer = 0
        For i As Integer = 0 To Me.EFlexFloor.Rows.Count - 2
            If Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(i).ArryBuilCorner.Length <= 2 Then
                MsgBox("顶角个数不能少于2个，请检查！")
                p = 1
                Exit For
            End If
        Next
        '判断污染源的坐标是不是重合
        For j As Integer = 0 To Me.BpipprmGale.ArryBuStack.Length - 1
            If Me.BpipprmGale.ArryBuStack(j).BrandX = 0 And Me.BpipprmGale.ArryBuStack(j).BrandY = 0 Then
                p = 1
            End If
        Next
        '判断每一层的顶角坐标是不是有重合
        Dim a As Integer = 0
        Dim b As Integer = 0
        If z = 0 Then
            For k As Integer = 0 To Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length - 1
                For h As Integer = k + 1 To BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner.Length - 1
                    If Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(k).x = Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(h).x Then
                        a = a + 1
                    End If
                    If Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(k).y = Me.BpipprmGale.ArryBldgi(xx - 1).ArryBuildTire(ClickFoor - 1).ArryBuilCorner(h).y Then
                        b = b + 1
                    End If
                Next
                If a >= 2 Or b >= 2 Then
                    Exit For
                End If
            Next
        End If
        '输出结果
        If z = 0 Then
            If a >= 2 Or b >= 2 Then
                MsgBox("坐标点有重合，请检查！")
            Else
                If p = 0 Then
                    Dim StrOutBp As String = ""
                    StrOutBp += Me.BpipprmGale.OutPutBp()
                    RichTextBox1.Text = StrOutBp
                End If
            End If
        End If
    End Sub

    Private Sub TxtTtle_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TxtTtle.Validating
        Me.BpipprmGale.Title = Me.TxtTtle.Text '标题
    End Sub

    Private Sub TxtUnitsN_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TxtUnitsN.Validating
        Me.BpipprmGale.UntisName = Me.TxtUnitsN.Text '单位名称
    End Sub

    Private Sub CTunitV_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles CTunitV.Validating
        Me.BpipprmGale.UntisVale = CTunitV.Value '单位数值
    End Sub

    Private Sub TxtGridN_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TxtGridN.Validating
        Me.BpipprmGale.GridName = Me.TxtGridN.Text '坐标处理标志
    End Sub

    Private Sub CTGridV_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles CTGridV.Validating
        Me.BpipprmGale.GridVale = Me.CTGridV.Value '坐标处理标志的值
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        ''MsgBox(My.Application.Info.DirectoryPath)
        ''执行aermod程序
        'If My.Computer.FileSystem.FileExists(PrjPath & "\" & strNameLast & "\" & "Bpipprm.exe") And My.Computer.FileSystem.FileExists(PrjPath & "\" & strNameLast & "\" & "bpipprm.BAT") Then

        '    Dim txtOut As String
        '    txtOut = Me.BpipprmGale.OutPutBp '获取控制流字符串
        '    '打开并写入文件。为了防止aermod占用文件，对访问的文件进行多次试试
        '    Dim IsBusy As Boolean = True
        '    While IsBusy
        '        Try
        '            FileOpen(1, PrjPath & "\" & strNameLast & "\" & "bpipprm.inp", OpenMode.Output)
        '            IsBusy = False
        '        Catch ex As Exception
        '            Thread.Sleep(1000)
        '        End Try
        '    End While
        '    PrintLine(1, txtOut)
        '    FileClose(1)
        '    DosCommandOutput = New DosCommand.DosCommandOutput
        '    DosCommandOutput.CreateNoWindow = False  '是否创建aermod窗体
        '    DosCommandOutput.Execute(PrjPath & "\" & strNameLast & "\", "bpipprm.BAT", "bp.txt")

        'Else
        '    MsgBox("建筑物下洗程序不存在，请重新安装程序！或与作者联系！")
        'End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'RichTextBox2.Text = Me.BpipprmGale.showStr()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Me.BpipprmGale.Process = Me.ComboBox1.Text '处理过程
    End Sub
End Class